package br.gov.dataprev.treinamento.ejb.modelo;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name="CLIENTES",uniqueConstraints={@UniqueConstraint(columnNames={"NO_EMAIL"})})
public class Cliente implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	@SequenceGenerator(name="SQ_CLIENTES")
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SQ_CLIENTES")
	@Column(name="ID_CLIENTE")
	private Long id;

	@Column(name="NO_EMAIL", length=50, nullable=false)
	private String email;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="DT_CADASTRO", updatable=false, nullable=false)
	private Date dataCadastro;
	
	@Column(name="NU_CPF", length=11, nullable=false)
	private String cpf;
	
	@Column(name="NO_CLIENTE", length=100, nullable=false)
	private String nome;
	
	@Temporal(TemporalType.DATE)
	@Column(name="DT_NASCIMENTO", nullable=true)
	private Date dataNascimento;
	
	@Enumerated(EnumType.STRING)
	@Column(name="CS_SEXO", nullable=false, length=10)
	private Sexo sexo;
	
	
	@OneToMany(fetch=FetchType.EAGER, orphanRemoval=true, cascade={CascadeType.ALL})
	@JoinColumn(name="ID_CLIENTE", nullable=false)
	private List<Telefone> telefones;
	
	@OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE} )
	@JoinColumn(name="ID_ENDERECO")
	private Endereco endereco;


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Date getDataCadastro() {
		return dataCadastro;
	}

	public void setDataCadastro(Date dataCadastro) {
		this.dataCadastro = dataCadastro;
	}

	public String getCpf() {
		return cpf;
	}

	public void setCpf(String cpf) {
		this.cpf = cpf;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public Date getDataNascimento() {
		return dataNascimento;
	}

	public void setDataNascimento(Date dataNascimento) {
		this.dataNascimento = dataNascimento;
	}

	public Sexo getSexo() {
		return sexo;
	}

	public void setSexo(Sexo sexo) {
		this.sexo = sexo;
	}

	public List<Telefone> getTelefones() {
		return telefones;
	}

	public void setTelefones(List<Telefone> telefones) {
		this.telefones = telefones;
	}

	public Endereco getEndereco() {
		return endereco;
	}

	public void setEndereco(Endereco endereco) {
		this.endereco = endereco;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Cliente))
			return false;
		Cliente other = (Cliente) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
